<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Minim : : GranulateSteady</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="stylesheet.css" rel="stylesheet" type="text/css">
</head>
<body> 
<center>
<table class="mainTable">
  <tr>
    <td class="header">
    	<span class="indexheader">Minim</span><br/>
    	<span class="indexnavigation">
    		<a href="index.html">core</a> | 
    		<a href="index_ugens.html">ugens</a> | 
    		<a href="index_analysis.html">analysis</a>
    	</span>
    </td>
    <td class="border-left">&nbsp;</td>
  </tr>
  
  <tr>
    <td class="classNavigation">
    	<p class="mainTextName">GranulateSteady</p>
    	
       <p class="linkListHeader">Fields</p>
       <p class="linkList">
           
    <a href="granulatesteady_field_audio.html" title="The default input is "audio."">audio</a><br/>
    
    <a href="granulatesteady_field_fadelen.html" title="Controls the length of the fade in and fade out.">fadeLen</a><br/>
    
    <a href="granulatesteady_field_grainlen.html" title="Controls the length of each grain.">grainLen</a><br/>
    
    <a href="granulatesteady_field_spacelen.html" title="Controls the space between each grain.">spaceLen</a><br/>
    
	   </p>
   
    	
       <p class="linkListHeader">Methods</p>
       <p class="linkList">
           
    <a href="granulatesteady_method_channelcount.html" title="Returns the number of channels this UGen has been configured to generate.">channelCount ( )</a><br/>
    
    <a href="granulatesteady_method_getlastvalues.html" title="Return the last values generated by this UGen. This will most often be
 used by sub-classes when pulling data from their inputs.">getLastValues ( )</a><br/>
    
    <a href="granulatesteady_method_patch.html" title="Send the output of this UGen to another UGen, UGenInput, or AudioOutput.">patch ( )</a><br/>
    
    <a href="granulatesteady_method_printinputs.html" title="Prints all inputs connected to this UGen (for debugging)">printInputs ( )</a><br/>
    
    <a href="granulatesteady_method_reset.html" title="Sets the state of this granulate to the very start of a grain. 
 Useful for syncing the granulate timing with other audio.">reset ( )</a><br/>
    
    <a href="granulatesteady_method_samplerate.html" title="Returns the sample rate of this UGen.">sampleRate ( )</a><br/>
    
    <a href="granulatesteady_method_setallparameters.html" title="Immediately sets all public class members to new values.">setAllParameters ( )</a><br/>
    
    <a href="granulatesteady_method_setalltimeparameters.html" title="Immediately sets all public class members concerning time to new values.">setAllTimeParameters ( )</a><br/>
    
    <a href="granulatesteady_method_setchannelcount.html" title="Let this UGen know how many channels of audio you will be asking it for.">setChannelCount ( )</a><br/>
    
    <a href="granulatesteady_method_setsamplerate.html" title="Set the sample rate for this UGen.">setSampleRate ( )</a><br/>
    
    <a href="granulatesteady_method_tick.html" title="Generates one sample frame for this UGen.">tick ( )</a><br/>
    
    <a href="granulatesteady_method_unpatch.html" title="Unpatch this UGen from an AudioOutput or other UGen.">unpatch ( )</a><br/>
    
	   </p>
   
    </td>
    <td class="mainText border-left">
    	A UGen which chops the incoming audio into steady grains
 of sound.  The envelope of these sounds has a linear fade
 in and fade out.
    	<p class="memberSectionHeader">Constructors</p>
    	<pre><em>Constructor for GranulateSteady.
 grainLength, length of each grain, defaults to 10 msec.
 spaceLength, space between each grain, defaults to 20 msec.
 fadeLength, length of the linear fade in and fade out of the grain envelope, defaults to 2.5 msec.
 minAmp, minimum amplitude of the envelope, defaults to 0.
 maxAmp, maximum amplitude of the envelope, defaults to 1.</em>
GranulateSteady()
<em>Constructor for GranulateSteady.
 minAmp, minimum amplitude of the envelope, defaults to 0.
 maxAmp, maximum amplitude of the envelope, defaults to 1.</em>
GranulateSteady(float grainLength, float spaceLength, float fadeLength)
<em>Constructor for GranulateSteady.</em>
GranulateSteady(float grainLength, float spaceLength, float fadeLength, float minAmp, float maxAmp)
</pre>
    	
   <p class="memberSectionHeader">Parameters</p>
   
        <span class="parameterName">grainLength</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: length of each grain in seconds</span><br/>
    
        <span class="parameterName">spaceLength</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: space between each grain in seconds</span><br/>
    
        <span class="parameterName">fadeLength</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: length of the linear fade in and fade out of the grain envelope in seconds</span><br/>
    
        <span class="parameterName">minAmp</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: minimum amplitude of the envelope</span><br/>
    
        <span class="parameterName">maxAmp</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: maximum amplitude of the envelope</span><br/>
    
   
    	<p class="memberSectionHeader">Related</p>
    	<A href="ugen_class_ugen.html">UGen</A><BR>
<A href="granulaterandom_class_granulaterandom.html">GranulateRandom</A><BR>

    	<p class="memberSectionHeader">Example</p>
    	<pre>/* granulateSteadyExample&lt;br/>
   is an example of using the Granulate Steady UGen inside an instrument.
   The steady granulation is used first to create rhythms, and then is used
   to create tones, effectively like a duty cycle amplitude modulation.
   &lt;p>
   For more information about Minim and additional features, 
   visit http://code.compartmental.net/minim/
   &lt;p>
   author: Anderson Mills&lt;br/>
   Anderson Mills's work was supported by numediart (www.numediart.org)
*/

// import everything necessary to make sound.
import ddf.minim.*;
import ddf.minim.ugens.*;

// create all of the variables that will need to be accessed in
// more than one methods (setup(), draw(), stop()).
Minim minim;
AudioOutput out;

/* SteadyGrainInstrument
   is an example of how to use GranulateSteady.  Basically it chops up a triangle wave
   into short bits given by the incoming period and the percentOn specifying the duty cycle.
*/

// Every instrument must implement the Instrument interface so 
// playNote() can call the instrument's methods.
class SteadyGrainInstrument implements Instrument
{
  // create all variables that must be used throughout the class
  GranulateSteady chopper;
  
  // constructor for this instrument
  SteadyGrainInstrument( float frequency, float amplitude, float period, float percentOn )
  { 
    // create new instances of any UGen objects as necessary
    // Need the triangle tone to chop up.
    Oscil toneOsc = new Oscil( frequency, amplitude, Waves.TRIANGLE );
    // need the GranulateSteady envelope
    chopper = new GranulateSteady( period*percentOn, period*( 1 - percentOn ), 0.0025 );
    
    // patch everything together up to the final output
    // the tone just goes into the chopper
    toneOsc.patch( chopper );
  }
  
  // every instrument must have a noteOn( float ) method
  void noteOn( float dur )
  {
    chopper.patch( out );
  }
  
  // every instrument must have a noteOff() method
  void noteOff()
  {
    chopper.unpatch( out );
  }
}

// setup is run once at the beginning
void setup()
{
  // initialize the drawing window
  size( 512, 200, P2D );

  // initialize the minim and out objects
  minim = new Minim( this );
  out = minim.getLineOut( Minim.MONO, 2048 );
  
  // pause time when adding a bunch of notes at once
  // This guarantees accurate timing between all notes added at once.
  out.pauseNotes();
  
  // I want a pause before the music starts
  out.setNoteOffset( 1.0 );
  
  // set the tempo for the piece
  // Since the parameters of GranulateSteady ore specified in seconds and not "beats"
  // it's easier to keep everything in the same reference frame by setting BPM to 60.
  out.setTempo( 60.0 );
  
  // The base hits for this section
  out.playNote( 0.000, 16.0, new SteadyGrainInstrument( 110f, 0.5, 1.0, 0.05 ) );
  out.playNote( 0.125, 16.0, new SteadyGrainInstrument( 110f, 0.5, 1.0, 0.05 ) );
 
  // The "snare" hits come in
  out.playNote( 4.500, 11.500, new SteadyGrainInstrument( 1320f, 0.3, 1.0, 0.03 ) );
  out.playNote( 4.625, 11.325, new SteadyGrainInstrument( 1320f, 0.3, 1.0, 0.03 ) );
  
  // Start doubling up the bass and snare in the 4th measure
  out.playNote( 7.250, 8.750, new SteadyGrainInstrument( 110f, 0.5, 4.0, 0.0125 ) );
  out.playNote( 7.375, 8.625, new SteadyGrainInstrument( 110f, 0.5, 4.0, 0.0125 ) );
  out.playNote( 7.750, 8.250, new SteadyGrainInstrument( 1320f, 0.3, 4.0, 0.0125 ) );
  out.playNote( 7.875, 8.125, new SteadyGrainInstrument( 1320f, 0.3, 4.0, 0.0125 ) );

  // At the beginning of the 8th measure add in a me-me-me-me  
  out.playNote( 8.00, 8.00, new SteadyGrainInstrument( 4840f, 0.1, 0.125, 0.50 ) );
  // And eventually put in a syncopated we-we-we-we which lasts past the section end
  out.playNote( 12.0625, 8.00, new SteadyGrainInstrument( 7260f, 0.06, 0.125, 0.75 ) );

  // start a new section --------------------------------------------------------------
  out.setNoteOffset( 16.0 );
 
  // Keep the bass hits going
  out.playNote( 0.000, 8.50, new SteadyGrainInstrument( 110f, 0.5, 2.0, 0.03 ) );
  out.playNote( 0.125, 8.50, new SteadyGrainInstrument( 110f, 0.5, 2.0, 0.03 ) );

  // create tones using the GranulateSteady.  This starts to happen when the grains
  // have a period of less than 50 msec.  At that rate, the grains start to be played
  // back at audio rate.
  out.playNote( 0.00, 4.00, new SteadyGrainInstrument( 9680f, 0.1, 0.042, 0.20 ) );
  out.playNote( 0.50, 4.00, new SteadyGrainInstrument( 7260f, 0.1, 0.031, 0.50 ) );
  out.playNote( 1.00, 4.00, new SteadyGrainInstrument( 4840f, 0.1, 0.031, 0.50 ) );
  out.playNote( 2.00, 4.00, new SteadyGrainInstrument( 2420f, 0.1, 0.020, 0.50 ) );
  out.playNote( 2.50, 4.00, new SteadyGrainInstrument( 1815f, 0.1, 0.010, 0.50 ) );
  out.playNote( 3.00, 4.00, new SteadyGrainInstrument( 1210f, 0.1, 0.010, 0.50 ) );
  out.playNote( 4.00, 4.00, new SteadyGrainInstrument( 550f, 0.1, 0.012, 0.50 ) );
  out.playNote( 5.002, 3.00, new SteadyGrainInstrument( 775f, 0.1, 0.012, 0.750 ) );
  out.playNote( 6.008, 2.00, new SteadyGrainInstrument( 775f, 0.2, 0.012, 0.750 ) );
  out.playNote( 7.00, 1.00, new SteadyGrainInstrument( 775f, 0.1, 0.012, 0.750 ) );
 
  // finally, resume time after adding all of these notes at once.
  out.resumeNotes();
}

// draw is run many times
void draw()
{
  // erase the window to grey
  background( 192 );
  // draw using a black stroke
  stroke( 0 );
  // draw the waveforms
  for( int i = 0; i &lt; out.bufferSize() - 1; i++ )
  {
    // find the x position of each buffer value
    float x1  =  map( i, 0, out.bufferSize(), 0, width );
    float x2  =  map( i+1, 0, out.bufferSize(), 0, width );
    // draw a line from one buffer position to the next for both channels
    line( x1, 50 + out.left.get(i)*50, x2, 50 + out.left.get(i+1)*50);
    line( x1, 150 + out.right.get(i)*50, x2, 150 + out.right.get(i+1)*50);
  }  
}
</pre>
    	<p class="memberSectionHeader">Usage</p>
    	Web & Application
    </td>
  </tr>
</table>
</center>
</body>
</html>